package com.ithiem.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.dubbo.api.ActivityApi;
import com.itheima.model.domain.*;
import com.ithiem.dubbo.mappers.ActivityMapper;
import com.ithiem.dubbo.mappers.UserInfoMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@DubboService
public class ActivityApiImpl implements ActivityApi {
   @Autowired
   private ActivityMapper activityMapper;

   @Autowired
   private UserInfoMapper userInfoMapper;

//    保存用户活动记录
    @Override
    public void save(Activity activity) {
        QueryWrapper<Activity> qw = new QueryWrapper<>();
        qw.eq("user_id",activity.getUserId());
        qw.eq("activity_date",activity.getActivityDate());
        qw.eq("activity_type",activity.getActivityType());
        Activity activityOne = activityMapper.selectOne(qw);
        if (activityOne==null){
            activity.setActivityCount(1);
            activityMapper.insert(activity);
        }else {
            activityOne.setActivityCount(activityOne.getActivityCount()+1);
            activityMapper.update(activityOne,qw);
        }

    }
//更新用户活动记录
    @Override
    public void update(Activity activity) {
      activity.setActivityCount(activity.getActivityCount()+1);
       activityMapper.updateById(activity);

    }
//    查询用户最新一次活动日期
@Override
public Activity queryBestNewDate(Long userId) {
       /* QueryWrapper<Activity> qw = new QueryWrapper<>();
        qw.eq("user_id",userId);
        qw.orderByDesc("updated");*/

    Activity activity = activityMapper.queryOne(userId);
    return activity;
}

//查询用户当天是否进行过活动---任意活动
    @Override
    public List<Activity> queryAcivity(Long userId, String nowDate) {
        QueryWrapper<Activity> qw = new QueryWrapper<>();
        qw.eq("user_id",userId);
        qw.eq("activity_date",nowDate);
        return activityMapper.selectList(qw);
    }

//    查询今天的指定类型的活动总数---登录
    @Override
    public Summary queryActivityCounts(String nowDate, String activityType) {
        Summary  summary= activityMapper.queryActivity(nowDate, activityType);

        return summary;
    }
    //查询今天的某项活动的记录--所有人的记录
    @Override
    public List<Activity> queryActivity(String nowDate, String activityType) {
        QueryWrapper<Activity> qw = new QueryWrapper<>();
        qw.eq("activity_date",nowDate);
        qw.eq("activity_type",activityType);
        return activityMapper.selectList(qw);
    }
    //查询某个人今天的某项活动的记录--单个记录
    @Override
    public Activity queryActivity(Long userId, String nowDate, String activityType) {
        QueryWrapper<Activity> qw = new QueryWrapper<>();
        qw.eq("user_id",userId);
        qw.eq("activity_date",nowDate);
        qw.eq("activity_type",activityType);
        return activityMapper.selectOne(qw);
    }


//        查询今天的所有用户的某一项活动
    @Override
    public Summary queryActiveUserCounts(String nowDate) {

        return activityMapper.queryActiveUserCounts(nowDate);
    }

    //每天的最后一个更新的记录
    @Override
    public List<Activity> queryActivityByDesc(String nowDate) {

        Integer date = Integer.valueOf(nowDate);
        List<Activity> activities = new ArrayList<>();
        for (Integer i = date ; i >=date-7 ; i--) {
//            查出每天最新一条数据和最晚一条数据
            String activityDate = String.valueOf(date);
            Activity activity = activityMapper.queryActivityByDesc(activityDate);
            if (activity!=null){
                activities.add(activity);
            }
        }
        return activities;
    }

    // 查找每月的新增用户---按时间查找->用户活动类型为注册的活动记录个数
    @Override
    public List<DashUserForSql> queryByDateRange(Integer year, String activityType) {
        List<DashUserForSql> dashUsers =  activityMapper.queryByDateRange(year,activityType);
        return dashUsers;
    }
    // 查找每月的活跃用户---按时间查找->只要用户有记录即可--任意活动
    @Override
    public List<DashUserForSql> queryByLastDateRange(Integer year) {
        List<DashUserForSql> dashUsers =  activityMapper.queryByLastDateRange(year);
        return dashUsers;
    }
// 次日留存
    @Override
    public DashUser queryActiveForMonth(Integer year, Integer month) {
        DashUser dashUser = activityMapper.queryActivieForMonth(year,month);
        return dashUser;
    }
    //查询指定创建时间的用户的年龄分布
    @Override
    public List<DashUser> queryAgeRangeByDate(String startDate, String endDate) {

        List<DashUser> dashUserList = activityMapper.queryAgeRangeByDate(startDate,endDate);
        if (CollUtil.isEmpty(dashUserList)){
            dashUserList = new ArrayList<>();
        }
        return dashUserList;
    }

    @Override
    public List<DashUser> queryWorkRangeByDate(String startDate, String endDate) {
        List<DashUser> dashUserList = activityMapper.queryWorkRangeByDate(startDate,endDate);
        if (CollUtil.isEmpty(dashUserList)){
            dashUserList = new ArrayList<>();
        }
        return dashUserList;

    }

    @Override
    public List<DashUser> querySexRangeByDate(String startDate, String endDate) {
        List<DashUser> dashUserList = activityMapper.querySexRangeByDate(startDate,endDate);
        if (CollUtil.isEmpty(dashUserList)){
            dashUserList = new ArrayList<>();
        }
        return dashUserList;
    }

    @Override
    public List<DashUser> queryLocalRangeByDate(String startDate, String endDate) {

        List<DashUser> dashUserList = activityMapper.queryLocalRangeByDate(startDate,endDate);
        if (CollUtil.isEmpty(dashUserList)){
            dashUserList = new ArrayList<>();
        }
        return dashUserList;
    }

}
